#include "crouteconditions.h"
#include "wnodemanager.h"
#include "../cmainframe.h"

initialiseSingleton(CRouteConditions);

CRouteConditions::CRouteConditions()
{

}

CRouteConditions::~CRouteConditions()
{

}

/// 解析Json
void CRouteConditions::parsingJson(cJSON *routecondi)
{
    if(routecondi == NULL) return;

    // 遍历数组中的每个对象
    cJSON* item = NULL;
    cJSON_ArrayForEach(item, routecondi)
    {
        tagRouteCondiItem pRouteCondiItem;

        // 解析 startlight
        cJSON* startlight = cJSON_GetObjectItem(item, "startlight");
        if (startlight && cJSON_IsString(startlight)) {
            pRouteCondiItem.startLight = startlight->valuestring;
        }

        // 解析 endlight
        cJSON* endlight = cJSON_GetObjectItem(item, "endlight");
        if (endlight && cJSON_IsString(endlight)) {
            pRouteCondiItem.endLight = endlight->valuestring;
        }

        // 解析 conflictsignal 数组
        cJSON* conflictsignal = cJSON_GetObjectItem(item, "conflictsignal");
        if (conflictsignal && cJSON_IsArray(conflictsignal)) {
            cJSON* signal = NULL;
            cJSON_ArrayForEach(signal, conflictsignal) {
                if (cJSON_IsString(signal)) {
                    WBaseNode *pBaseNode = WNodeManager::getSingleton().getNodeByName(signal->valuestring);
                    if(pBaseNode && pBaseNode->getNodeType() == NodeType::NODE_SIGNAL)
                        pRouteCondiItem.conflictsignals.push_back(static_cast<WSignalLampNode*>(pBaseNode));
                }
            }
        }

        // 解析 conflictswitch 数组
        cJSON* conflictswitch = cJSON_GetObjectItem(item, "conflictswitch");
        if (conflictswitch && cJSON_IsArray(conflictswitch)) {
            cJSON* sw = NULL;
            cJSON_ArrayForEach(sw, conflictswitch) {
                if (cJSON_IsObject(sw)) {
                    cJSON* child = sw->child;
                    while (child) {
                        if (cJSON_IsString(child)) {
                            WBaseNode *pBaseNode = WNodeManager::getSingleton().getNodeByName(child->string);
                            if(pBaseNode && pBaseNode->getNodeType() == NodeType::NODE_SWITCH)
                            {
                                pRouteCondiItem.conflictswitch.push_back(tagSwitchCondiItem(static_cast<WSwitchNode*>(pBaseNode),
                                                                                            wxString(child->valuestring) == "定位" ? SwitchState::POSITIVE : SwitchState::NEGATIVE));
                            }
                        }
                        child = child->next;
                    }
                }
            }
        }

        // 解析 followswitch 数组
        cJSON* followswitch = cJSON_GetObjectItem(item, "followswitch");
        if (followswitch && cJSON_IsArray(followswitch)) {
            cJSON* sw = NULL;
            cJSON_ArrayForEach(sw, followswitch) {
                if (cJSON_IsObject(sw)) {
                    cJSON* child = sw->child;
                    while (child) {
                        if (cJSON_IsString(child)) {
                            WBaseNode *pBaseNode = WNodeManager::getSingleton().getNodeByName(child->string);
                            if(pBaseNode && pBaseNode->getNodeType() == NodeType::NODE_SWITCH)
                            {
                                pRouteCondiItem.followswitch.push_back(tagSwitchCondiItem(static_cast<WSwitchNode*>(pBaseNode),
                                                                                            wxString(child->valuestring) == "定位" ? SwitchState::POSITIVE : SwitchState::NEGATIVE));
                            }
                        }
                        child = child->next;
                    }
                }
            }
        }

        // 添加一个进路条件到系统中
        this->addRouteCondiItem(pRouteCondiItem);
    }
}

/// 添加一个进路条件到系统中
void CRouteConditions::addRouteCondiItem(tagRouteCondiItem pRouteCondiItem)
{
    m_RouteConditions[pRouteCondiItem.startLight][pRouteCondiItem.endLight] = pRouteCondiItem;
}

/// 得到指定进路的敌对条件
const tagRouteCondiItem* CRouteConditions::getRouteCondiItem(wxString startNode,wxString endNode)
{
    if(startNode == "" || endNode == "" || m_RouteConditions.empty())
        return NULL;

    std::map<wxString,std::map<wxString,tagRouteCondiItem>>::iterator iter = m_RouteConditions.find(startNode);
    if(iter == m_RouteConditions.end())
        return NULL;

    std::map<wxString,tagRouteCondiItem>::iterator iter2 = (*iter).second.find(endNode);
    if(iter2 == (*iter).second.end())
        return NULL;

    return &(*iter2).second;
}
